#include "Tc_AllTasks.h"
#include "../Protocols/ElectricOilPump_24V_ForCan_ForMcu.h"
#include "../McuDrivers/system/system.h"
#include "../McuDrivers/dma/dma.h"
#include "../McuDrivers/flash/DEE_Emulation_16-bit.h"
#include "../McuDrivers/can/can.h"
#include "../McuDrivers/gpio/gpio.h"
#include "../McuDrivers/uart/uart.h"
#include "../McuDrivers/timer/timer.h"

#include "../McuDrivers/adc/adc.h"
#include "../McuDrivers/watchdog/watchdog.h"
#include "../GlobalValuesDefine.h"
#include "../Protocols/ElectricOilPump_SpecialParse.h"
#include "../OtherDevices/A4964.h"



//CAN接收超时标识，数值代表超时的秒数
uint8_t m_CAN_ReceiveTimeoutFlag = 0;

uCAN1_MSG m_ReceiveCanMsg; //can接收信息存储结构体


//系统初始化任务
void Tasks_SystemInitial_g()
{
    /******** Initial ********/
    System_Initialize_g();

    InnerEEPromDataInit();
    
    
    SPI2_Initial_g();
    DMA_Initialize_g();
    ECAN1_Initialize_g();
    GPIO_Initial_g();
    Timer1_Initial_g();
    //Timer23_Initial_g();
    //Uart_Initial(115200);
    ADC1_Initialize_g();
    
    //GPIO_A4964_FeedWatchDo_Toggle_g();
    
    //GPIO_LED_Reserve_Write_g(1);

    Sp_A4964RegisterRead_g();
    GPIO_A4964_LG_Enable_g();
    
    System_Delay_MS_g(200);
    
    
    A4964_Initial_g();
    A4964_ClearFault_g();
    A4964_WriteSpeed_g(1,0);
    A4964_WriteRunEnable_g(1,0);
//    System_Delay_MS_g(200);
//    A4964_WriteSpeed_g(0,1200);
//    A4964_WriteRunEnable_g(0,1);
//    System_Delay_MS_g(2000);
    /********************/
    

    /***************************/
    
    Timer1_Start_g(10);
    WatchDog_Start_g(); //watchdog enable
}

//系统结束运行操作
void Tasks_SystemFinished_g()
{
    Timer1_Stop_g();
}

/********************* for communication **************************/
//while里一直执行,接收can数据
void Tasks_ReceiveCan_g(void)
{
    if(ECAN1_ReceiveCanMsg_g(&m_ReceiveCanMsg))
    {
        if(Sp_CanReceiveProcess_g(&m_ReceiveCanMsg)==1)
        {
            m_CAN_ReceiveTimeoutFlag=0;
            
        }
    }
    
}

//发送帧1，模式、转矩、转速、故障
void Tasks_CanSendFrame1_g(void)
{
    //forbid motor frame
    if(g_ForbidMotorFrame==1)
        return;
    
    uCAN1_MSG sendCanMsg; //can发送信息存储结构体
    sendCanMsg=Sp_CanSendFrame1_g();
    ECAN1_Transmit_g(ECAN1_PRIORITY_MEDIUM, &sendCanMsg);
}

//发送帧2、3、4，电流电压、功率温度、软件版本
void Tasks_CanSendFrame234_g(void)
{          
    //forbid motor frame
    if(g_ForbidMotorFrame==1)
        return;
    
    uCAN1_MSG sendCanMsg; //can发送信息存储结构体
    sendCanMsg = Sp_CanSendFrame2_g();
    ECAN1_Transmit_g(ECAN1_PRIORITY_MEDIUM, &sendCanMsg);
    
    sendCanMsg = Sp_CanSendFrame3_g();
    ECAN1_Transmit_g(ECAN1_PRIORITY_MEDIUM, &sendCanMsg);
    
    sendCanMsg =Sp_CanSendFrame4_g();
    ECAN1_Transmit_g(ECAN1_PRIORITY_MEDIUM, &sendCanMsg);
}

//超过2s速度下降，同时报CAN通讯故障
void Tasks_CanReceiveTimeoutCount_g(void)
{
    //forbid motor frame
    if(g_ForbidMotorFrame==1)
        return;
    
    /***** CAN超时计算 *********/
    m_CAN_ReceiveTimeoutFlag++;
    Sp_CanReceiveTimeoutCheck_g(m_CAN_ReceiveTimeoutFlag);
}

//速度变化
void Tasks_SpeedChange_g(void)
{
    //forbid motor frame
    if(g_ForbidMotorFrame==1)
        return;
    
    Sp_SpeedChange_g(); //速度变化
}

void Tasks_WatchDogClear_g(void)
{
    WatchDog_Clear_g(); //clear watchdog counter
    Sp_A4964FeedWatchDog_g();
}

void Tasks_GpioToggle_g(void)
{
    GPIO_LED_MCU_Running_Toggle_g();
    //GPIO_LED_Reserve_Toggle_g();
}

//ADC转换任务
void Tasks_AdcConvert_g(void)
{
    Sp_AdcConvert_g();
}

void Tasks_A4964_State_g()
{
    Sp_GetA4964State_g();
}